/**
 * 
 */
package com.buss.core.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.buss.core.model.Role;
import com.buss.core.model.Sysmenu;
import com.buss.core.persistence.OrderBy;
import com.buss.core.persistence.ResultList;
import com.buss.core.service.RoleService;
import com.buss.core.sysparam.SysStaticParam;
import com.buss.core.validator.ResultMessage;

/**
 * @author 高小勇
 * 创建时间:2011-5-28
 * wcms2.0
 */
@RequestMapping("/admin")
@Controller
public class RoleController extends BaseController {

	@Autowired
	private RoleService roleService;
	
	/**
	 * 角色管理列表页面
	 * @return
	 */
	@RequestMapping("/role/page")
	public ModelAndView listPage(@RequestParam(required=false) String menuid){
		ModelAndView view = new ModelAndView("role/listpage");
		view.addObject(SysStaticParam._MENUID_NAME, menuid);
		return view;
	}
	
	/**
	 * 新增、修改页面
	 * @param type
	 * @param id
	 * @return
	 */
	@RequestMapping("/role/{type}")
	public ModelAndView editPage(@PathVariable String type,@RequestParam(required=false) Long id){
		ModelAndView view = new ModelAndView("/role/edit");
		Role role = new Role();
		if("editpage".equals(type)){
			role = roleService.loadRole(id);
		}
		view.addObject("role",role);
		return view;
	}
	
	/**
	 * 设置角色权限信息页面
	 * @param roleId
	 * @return
	 */
	@RequestMapping("/role/permis/{roleId}")
	public ModelAndView editPermisPage(@PathVariable Long roleId){
		ModelAndView view = new ModelAndView("/role/edit_permis");
		view.addObject("roleId", roleId);
		return view;
	}
	
	/**
	 * 新增角色信息
	 * @param role
	 * @return
	 */
	@RequestMapping("/role/add")
	@ResponseBody
	public Map<String,Object> add( Role role,HttpServletRequest request){
		role.setCompanyId(getCompanyId(request));
		roleService.saveRole(role);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("message", new ResultMessage("0", "新增角色信息成功!"));
		return map;
	}

	/**
	 * 修改角色信息
	 * @param role
	 * @return
	 */
	@RequestMapping("/role/edit")
	@ResponseBody
	public Map<String,Object> edit( Role role,HttpServletRequest request){
		role.setCompanyId(getCompanyId(request));
		roleService.editRole(role);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("message", new ResultMessage("0", "修改角色信息成功!"));
		return map;
	}
	
	@Override
	@RequestMapping("/role/delete")
	@ResponseBody
	public Map<String, Object> delete(Long[] ids) {
		roleService.deleteRole(ids);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("message", new ResultMessage("0", "删除角色信息成功!"));
		return map;
	}
	
	/**
	 * 获取角色信息列表
	 * @return
	 */
	@RequestMapping("/role/list")
	@ResponseBody
	public Map<String,Object> list(String orderField,String orderType, HttpServletRequest request){
		OrderBy orderBy = new OrderBy();
		orderBy.add(orderField, orderType);
		ResultList<Role> resultList = roleService.queryRole(getCompanyId(request),orderBy);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", resultList);
		map.put("message", new ResultMessage("0", "获取角色列表信息成功!"));
		return map;
	}
	
	@RequestMapping("/role/listByUserId/{userId}")
	@ResponseBody
	public Map<String,Object> listByUserId(String orderField,String orderType,@PathVariable Long userId){
		OrderBy orderBy = new OrderBy();
		orderBy.add(orderField, orderType);
		ResultList<Role> resultList = roleService.queryRolesByUser(userId,orderBy);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put(SysStaticParam._RESULT, resultList);
		map.put(SysStaticParam._MESSAGE, new ResultMessage(SysStaticParam._RESULT_SUCCESS, "获取角色信息成功!"));
		return map;
	}
	
	/**
	 * 更新角色拥有菜单权限信息
	 * @param role
	 * @return
	 */
	@RequestMapping("/role/updateRoleMenu")
	@ResponseBody
	public Map<String,Object> updateRoleMenu(@RequestParam(required=false) Long roleId,Long[] menuIds){
		List<Sysmenu> menus = new ArrayList<Sysmenu>();
		if(menuIds == null){
			menuIds = new Long[]{};
		}
		for(Long menuId : menuIds){
			Sysmenu menu = new Sysmenu();
			menu.setId(menuId);
			menus.add(menu);
		}
		Role role = new Role();
		role.setSysmenus(menus);
		role.setId(roleId);
		roleService.updateRoleMenu(role);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put(SysStaticParam._MESSAGE, new ResultMessage("0", "更新角色拥有菜单功能权限信息成功!"));
		return map;
		
	}
	
}
